﻿Imports LRS.BLL
Imports LRS.DTO
Imports System.Windows.Forms

Public Class ManageEmployee
    Private oldRowIndex As Integer = -1
    Private IsAdding As Boolean = False
    Private Sub ManageEmployee_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        gridEmployee.AutoGenerateColumns = False
        gridEmployee.DataSource = bEmployee.GetEmployees()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        gridEmployee.Enabled = False
        btnDelete.Enabled = False
        btnAdd.Enabled = False
        btnCancel.Enabled = True
        IsAdding = True
        ucEmp.ClearText()
        ucEmp.Enabled = True
    End Sub

    Private Sub gridEmployee_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridEmployee.RowEnter
        If (oldRowIndex = e.RowIndex) Then
            Return
        End If
        ucEmp.Enabled = True
        oldRowIndex = e.RowIndex
        Dim emp As eEmployee = bEmployee.GetEmployeeByID(gridEmployee.Rows(e.RowIndex).Cells("EmployeeID").Value)
        If (Not emp Is Nothing) Then
            ucEmp.SetEmployee(emp)
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If (Not ucEmp.ucValidateEmp()) Then
            Return
        End If
        Dim emp As eEmployee = ucEmp.GetEmployee()
        If (IsAdding) Then
            If (emp.Password.Trim.Length = 0) Then
                MessageBox.Show("Chọn mật khẩu cho nhân viên này")
                Return
            End If
            'Add new employee
            bEmployee.InsertEmployee(emp)
        Else
            'Update employee
            If (emp.Password.Length <> 0) Then
                bEmployee.UpdatePassword(emp.EmployeeID, emp.Password)
            End If
            bEmployee.UpdateEmployee(emp)
        End If
        gridEmployee.Enabled = True
        btnAdd.Enabled = True
        btnDelete.Enabled = True
        ucEmp.Enabled = False
        btnCancel.Enabled = False
        IsAdding = False
        oldRowIndex = -1
        gridEmployee.DataSource = Nothing
        gridEmployee.DataSource = bEmployee.GetEmployees()
        Dim i = oldRowIndex
        oldRowIndex = -1
        gridEmployee.Rows(i).Cells("EmployeeNumber").Selected = True
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        gridEmployee.Enabled = True
        btnAdd.Enabled = True
        btnDelete.Enabled = True
        ucEmp.Enabled = False
        btnCancel.Enabled = False
        IsAdding = False
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Dim row As Integer
        If (gridEmployee.SelectedCells.Count = 0) Then
            MessageBox.Show("Vui lòng chọn dòng cần xóa")
            Return
        End If
        row = gridEmployee.SelectedCells(0).RowIndex
        Dim result As DialogResult
        result = MessageBox.Show("Bạn có thật sự muốn xóa nhân viên mang số hiệu: " & gridEmployee.Rows(row).Cells("EmployeeNumber").Value, "Xác nhận", MessageBoxButtons.OKCancel)
        If (result = DialogResult.OK) Then
            Dim EmpID As Integer = gridEmployee.Rows(row).Cells("EmployeeID").Value
            bEmployee.DeleteEmployeeByID(EmpID)
            gridEmployee.DataSource = Nothing
            gridEmployee.DataSource = bEmployee.GetEmployees()
            oldRowIndex = -1
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        gridEmployee.DataSource = Nothing
        gridEmployee.DataSource = bEmployee.GetEmployeesByKeyword(txtSearch.Text)
    End Sub
End Class